<template>
  <div>
    <el-form :model="nonDrugHistory" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="治疗方法" prop="treatmentUsage">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          show-word-limit
          maxlength="300"
          v-model="nonDrugHistory.treatmentUsage"
          placeholder="请输入治疗方法"
        ></el-input>
      </el-form-item>

      <el-form-item label="治疗频次" prop="treatmentFrequency">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          show-word-limit
          maxlength="200"
          v-model="nonDrugHistory.treatmentFrequency"
          placeholder="请输入治疗频次[一年几次/一个月几次]"
        ></el-input>
      </el-form-item>

      <el-form-item label="治疗日期" prop="treatmentTime">
        <el-date-picker
          style="width:320px;"
          @change="treatmentTimeChange"
          v-model="nonDrugTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="是否继续" v-if="hasContinue">
        <el-switch :active-value="1" :inactive-value="0" v-model="nonDrugHistory.isContinue"></el-switch>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    hasContinue: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      nonDrugTime: [],
      nonDrugHistory: {
        endTime: "",
        isContinue: 0,
        startTime: "",
        treatmentFrequency: "",
        treatmentUsage: ""
      },
      dataRule: {
        treatmentUsage: [
          {
            required: true,
            message: "治疗采用方法必须填写",
            trigger: "blur"
          }
        ],
        treatmentFrequency: [
          {
            required: true,
            message: "治疗频次必须填写",
            trigger: "blur"
          }
        ],
        treatmentTime: [
          {
            validator: (rule, value, callback) => {
              if (
                !this.nonDrugHistory.startTime ||
                !this.nonDrugHistory.endTime
              ) {
                callback(new Error("药物使用时间必须选择"));
              } else {
                callback();
              }
            },
            required: true,
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  //计算属性 类似于 data 概念
  computed: {},

  //方法集合
  methods: {
    putNonDrugHistory(value) {
      this.nonDrugHistory = value;
      this.nonDrugTime.push(value.startTime);
      this.nonDrugTime.push(value.endTime);
    },
    // 非药物使用日期选择
    treatmentTimeChange(value) {
      if (value != null && value.length == 2) {
        this.nonDrugHistory.startTime = value[0];
        this.nonDrugHistory.endTime = value[1];
      } else if (value == null) {
        this.nonDrugHistory.startTime = null;
        this.nonDrugHistory.endTime = null;
        this.nonDrugTime = null;
      }
    },
    submit(value) {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          // 调用父组件的赋值方法
          if (value == "koaNonDrugHistory") {
            // 如果父组件传来的是‘koaDrugHistory’，说明是koa
            this.nonDrugHistory.isKoa = 1;
          } else {
            this.nonDrugHistory.isKoa = 0;
          }
        }
      });
      if (this.nonDrugHistory.isKoa == 0 || this.nonDrugHistory.isKoa == 1) {
        return this.nonDrugHistory;
      } else {
        return null;
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {}
};
</script>
<style scoped>
</style>